package com.syn.michat.util;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Provider;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;

/**
 * Blowfish算法加解密工具类
 */

public class BlowfishUtil {

	// 密钥
    private static final String ENCRYPT_KEY           = "1230fc0095784f4acg249b8996445f40";
    // 初始化向量
    private static final String INITIALIZATION_VECTOR = "a89a7122";
    // 转换模式
    private static final String ALGORITHM        = "Blowfish/CBC/PKCS5Padding";
    // 密钥算法名称
    private static final String BLOWFISH              = "Blowfish";

    public static String encrypt(String value) throws Exception {
		try {
			Provider sunJce = new com.sun.crypto.provider.SunJCE();
			Security.addProvider(sunJce);
			SecretKeySpec skeySpec = new SecretKeySpec(ENCRYPT_KEY.getBytes(), "Blowfish");
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			AlgorithmParameterSpec iv = new IvParameterSpec(INITIALIZATION_VECTOR.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec,iv);

			return Base64.encodeBase64String(cipher.doFinal(value.getBytes()));
		} catch (Exception ex) {
			throw new Exception(ex);
		}
    }

    public static String decrypt(String value) throws Exception {
		try {
			Provider sunJce = new com.sun.crypto.provider.SunJCE();
			Security.addProvider(sunJce);
			SecretKeySpec skeySpec = new SecretKeySpec(ENCRYPT_KEY.getBytes(), "Blowfish");
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			AlgorithmParameterSpec iv = new IvParameterSpec(INITIALIZATION_VECTOR.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			
			return new String(cipher.doFinal(Base64.decodeBase64(value)));
		} catch (Exception ex) {
			throw new Exception(ex);
		}
    }

    public static void main(String[] args) throws Exception {
    	String abcd = BlowfishUtil.encrypt("{content={folder={name=测试空间6655, uuid=05189151351794274979954841221277}, rights={users=[{head_url=12353801918113584426290459945094.jpg, nick_name=lwttdad, rights=1,4,5,6, uuid=45076641529182362987962194064734}, {head_url=12353801918113584426290459945094.jpg, nick_name=lwttdad, rights=1,4,5,6, uuid=45076641529182362987962194064734}]}}, sender=20038810248654369847023593287601, receiver=49898092579282691465659324751838, receive_type=2, sub_type=m_1850}");
    	System.out.println(abcd);

		String a = "BFLrXUZCAAFrqcX4XhXiugOvFMNU3HqlH6JGR7RAVjjDanV8Uzc/uQFJ5V/spIJwya9/b9rnckyI6aoBhXutML5AmiuJtlE+fEtKko/hIxjIjpMXxkFdM90ut6JJe1d4esoP3W0T1PjxmiEDWZiHg29rZQ/aKJcfFrfHIyy11mlonTrcJN79wuhPkKt0IOo44fjqxF4ENbzMA2MQTAGvJZinEvJQbcIdCcYd+6Uo1wJjaso1wg4pV1KTjrTsZN2nDn3RTzzS9SVHmgoh8ohXjs7Onidbw5PdTq/18atzRi7ZIThxAtGmBtyINIpUbDUvNJ4B0pURmoe+5o6C/n82tZpGl4Ju4nQzVCI6Rk7YWiJytTH+mERlxLUKxjuIBIO/T96ortiH8UOQAc5MtgiAytRiKBbO0B2LnsQQq4dbUGwP06nw44h1LjqEYwRtSkvxSxMSor5ZxPYu7mJJufQ37xWNEEexmehwWUHreC2PDYOJBzrv5pwCpmlPs7nW97KdMkjT5qXZEjDUGFNQdCAsHZZ1DfPM6lf5gM0m7G1K+Yilzz1ANHDrV+vJKVOH10aQ6p+DY23w0a+BVyJuaSjKy1/tkrqAi/FeHpSL/utt6rQ=";
    	String bck = BlowfishUtil.decrypt(a);
    	System.out.println(bck);
 	}
    
}
